/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Author:
 *     coolair.loveU2@gmail.com
 *******************************************************************************/

package org.skyfree.ghyll.tcard.core.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.skyfree.ghyll.tcard.core.IStoragePath;
import org.skyfree.ghyll.tcard.core.ITProject;
import org.skyfree.ghyll.tcard.core.ITProjectDescription;
import org.skyfree.ghyll.tcard.core.ITWorkpiece;
import org.skyfree.ghyll.tcard.core.TCardCore;

/**
 * This class ...
 */

public class TProject extends AbstractTResource implements ITProject
{
	TWorkspace workspace;
	ITProjectDescription desc;
	boolean exist = false;
	String name;
	HashMap<String, ITWorkpiece> cacheWorkPiece;
	
	public TProject(IStoragePath path,String name)
	{
		super(path);
		this.name = name;
		this.desc = new TProjectDescription(this.path);
		this.desc.setName(this.name);
	}

	@Override
	public void create(ITProjectDescription description) throws CoreException
	{
		if(this.exist)
		{
			throw new CoreException(new Status(IStatus.ERROR,TCardCore.PLUGIN_ID,"Can't create project that exist :"+this.path));
		}
		else
		{
			if(this.workspace.exist(description.getName()))
			{
				throw new CoreException(new Status(IStatus.ERROR,TCardCore.PLUGIN_ID,"Can't create project that exist :"+this.path));
			}
			else
			{
				this.desc = description;
				this.desc.setStatus(ITProject.STATUA_OPEN);
				this.save();
				this.exist = true;
				this.workspace.add2Workspace(this);
			}
		}

	}

	@Override
	public ITProjectDescription getDescription()
	{
		return this.desc;
	}

	@Override
	public String getName()
	{
		return this.desc.getName();
	}

	@Override
	public String getStatus()
	{
		return this.desc.getStatus();
	}

	@Override
	public boolean isClosed()
	{
		return this.getStatus().equalsIgnoreCase(ITProject.STATUA_CLOSE);
	}

	@Override
	public void setDescription(ITProjectDescription description)
			throws CoreException
	{
		this.desc = description;

	}

	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Class adapter)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void load() throws CoreException
	{
		this.desc.load();
		this.exist = true;
	}

	@Override
	public void save() throws CoreException
	{
		this.desc.save();

	}

	@Override
	public void reload() throws CoreException
	{
		this.desc.reload();
		this.exist = true;
		
	}

	@Override
	public String toString()
	{
		return this.getPath()+"	"+this.desc.getName()+"	"+this.desc.getContractID()+" "+this.desc.getComment();
	}

	@Override
	public ITWorkpiece[] getAllWorkpiece()
	{
		if(this.cacheWorkPiece==null)
		{
			this.cacheWorkPiece = new HashMap<String, ITWorkpiece>();
			String[] allWorkPiece = this.provider.getWorkPiece(this);
			for(int i=0;i<allWorkPiece.length;i++)
			{
				TWorkpiece wp = new TWorkpiece(this.path.append(allWorkPiece[i]),allWorkPiece[i]);
				
				wp.project = this;
				try
				{
					wp.load();
				} catch (CoreException e)
				{
					this.log(e);
				}
				this.cacheWorkPiece.put(allWorkPiece[i], wp);
			}
		}
		return this.cacheWorkPiece.values().toArray(new ITWorkpiece[0]);
	}
	
	@Override
	public ITWorkpiece getWorkpiece(String name)
	{
		ITWorkpiece value = this.cacheWorkPiece.get(name);
		if(value==null)
		{
			TWorkpiece wp = new TWorkpiece(this.path.append(name),name);
			wp.project = this;
			return wp;
		}
		else
		{
			if(!value.isLoaded())
			{
				try
				{
					value.load();
				} catch (CoreException e)
				{
					this.log(e);
				}
			}
			return value;
		}
	}

	@Override
	public boolean exist(String wpName)
	{
		this.getAllWorkpiece();
		return this.cacheWorkPiece.containsKey(wpName);
	}

	@Override
	public void delete() 
	{
		try {
			this.provider.delete(this);
			this.workspace.cacheProject.remove(this.name);
		} catch (Exception e) {
			this.log(new CoreException(new Status(IStatus.ERROR,TCardCore.PLUGIN_ID,e.getMessage())));
		}
		
	}

	@Override
	public void importWorkpieces(ArrayList<ITWorkpiece> toImport) 
	{
		Iterator<ITWorkpiece> it = toImport.iterator();
		while(it.hasNext())
		{
			ITWorkpiece one = it.next();
			
			one.setStorageType(ITWorkpiece.STT_Link);
			try {
				if(this.provider.createLink(this,one))
				{
					String name = one.getName();
					ITWorkpiece wp = (ITWorkpiece) TCardCore.create(ITWorkpiece.class, 
							this.getPath().append(one.getName()));
					wp.setName(name);
					wp.load();
					wp.setProject(this);
					this.cacheWorkPiece.put(name,wp);
				}
			} catch (Exception e)
			{
				this.log(new CoreException(new Status(IStatus.ERROR,TCardCore.PLUGIN_ID,e.getMessage())));
			}
					
		}
		
	}

	@Override
	public void exportWorkpiece2Repository(ITWorkpiece wp,String category) 
	{
		try {
			this.provider.move2Repository(wp,category);
		} catch (Exception e) {
			this.log(new CoreException(new Status(IStatus.ERROR,TCardCore.PLUGIN_ID,e.getMessage())));
		}
	}
	
}
